#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include "parser.h"
#include "executor.h"
#include "builtins.h"
#include "signals.h"

static int foreground_mode = 1;
static pid_t foreground_pgid = 0;

void init_shell() {
    setup_signal_handlers();
}

void shell_loop() {
    char *line;
    Command *commands;
    int status = 1;

    do {
        printf("mysh> ");
        fflush(stdout);

        line = read_line();
        if (!line) continue;

        commands = parse_line(line);
        if (!commands) continue;

        if (is_builtin(commands)) {
            execute_builtin(commands);
        } else {
            execute_commands(commands, &foreground_pgid, &foreground_mode);
        }

        free_commands(commands);
        free(line);
    } while (status);
}
